import { state } from "../config.js";
import { currentRole, role1, role2 } from "../index.js";
import { allRolePosition } from "../role.js";
import { scoket } from "../utils/scoket.js";
import { removeKey, setKey } from "./scheduler.js";
export var getPosition = function (ele) {
    var po = ele.style.transform;
    var xy = po.slice(po.indexOf("(") + 1, po.indexOf(")")).split(",");
    return {
        x: +xy[0].replace("px", ""),
        y: +xy[1].replace("px", "")
    };
};
//按键触发移动
var keyword = ["a", "s", "d", "w", "j"];
var lastKey = "";
export function transfrom(e) {
    //如果当前状态是暂停则不进行下一步操作
    if (state.stop)
        return;
    if (!keyword.includes(e.key))
        return;
    if (lastKey === e.key)
        return;
    lastKey = e.key;
    setKey(e.key);
}
scoket.$on(function (res) {
    if (res.path !== "/key/down")
        return;
    if (res.role === "p1") {
        role1.style.transform = setTransfrom(res.value.rolePosition);
        allRolePosition.p1 = res.value.rolePosition;
    }
    if (res.role === "p2") {
        role2.style.transform = setTransfrom(res.value.rolePosition);
        allRolePosition.p2 = res.value.rolePosition;
    }
});
//停止移动
export function stopTransform(e) {
    if (!keyword.includes(e.key))
        return;
    lastKey = "";
    removeKey(e.key);
}
export var getRolePosition = function () {
    return getPosition(currentRole);
};
export var setTransfrom = function (po) {
    return "translate(".concat(po.x, "px, ").concat(po.y, "px)");
};
export var setPosition = function (ele, po) {
    ele.style.transform = setTransfrom(po);
};
